﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace KCL.Code.Data
{
    public class ThicknessDAO : BaseDAO
    {
        public List<Thickness> GetAllThicknesses()
        {
            try
            {
                KCLDataContext ctx = new KCLDataContext();
                var lstThicknesses = from t in ctx.Thicknesses
                                where t.isActive == true
                                orderby t.thick
                                select t;
                return lstThicknesses.AsEnumerable().ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public bool Insert(Thickness t)
        {
            try
            {
                KCLDataContext ctx = new KCLDataContext();
                ctx.Thicknesses.InsertOnSubmit(t);
                ctx.SubmitChanges();
                return true;
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                if (ex.Number == 2627)
                {
                    try
                    {
                        KCLDataContext ctx = new KCLDataContext();
                        var thicknessToRestore = (from th in ctx.Thicknesses
                                              where th.thick.ToUpper() == t.thick.ToUpper()
                                              && th.isActive == false
                                              select th).FirstOrDefault();
                        thicknessToRestore.thick = t.thick;
                        thicknessToRestore.isActive = true;
                        ctx.SubmitChanges();
                        return true;
                    }
                    catch (Exception)
                    {
                        return false;
                    }
                }
                return false;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Update(Thickness t)
        {
            try
            {
                KCLDataContext ctx = new KCLDataContext();
                var thicknessToUpdate = (from th in ctx.Thicknesses
                                     where th.id == t.id
                                     select th).FirstOrDefault();
                thicknessToUpdate.thick = t.thick.Trim();
                thicknessToUpdate.isActive = t.isActive;
                if (thicknessToUpdate.thick.Length == 0)
                    return false;
                ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Delete(Thickness t)
        {
            try
            {
                KCLDataContext ctx = new KCLDataContext();
                var thicknessToDelete = (from th in ctx.Thicknesses
                                     where th.id == t.id
                                     select th).FirstOrDefault();
                thicknessToDelete.isActive = false;
                ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}